<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" charset="utf-8">
		var lastIndex,id = '${id}';
		$(function() {
			var irid = $('#transferForm').find('input[name=irid]');
			var iridCombobox = irid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});

			var orid = $('#transferForm').find('input[name=orid]');
			var oridCombobox = orid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});
			
			var uid = $('#transferForm').find('input[name=uid]');
			var uidCombobox = uid.combobox({
				url : '${pageContext.request.contextPath}/qxgl/userAction!noAuth_userInReperCombobox.html',
				valueField : 'id',
				textField : 'username',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});
			
			$.ajax({
				url : '${pageContext.request.contextPath}/serial.html',
				data : {sign : 'DB'},
				type : "POST",
				success : function(d){
					$('#serial').val(d);
				}
			});
			
			$.ajax({
				url : '${pageContext.request.contextPath}/kcgl/goodsTransferAction!noAuth_search.html',
				data : {id : id},
				type : "POST",
				success : function(d){
					alert(d);
					var json = $.parseJSON(d);
					var f = $('#transferForm');
					f.form("load",{
						id : json[0].id,
						date : json[0].date,
						serial : json[0].serial,
						uid : json[0].uid,
						irid : json[0].irid,
						orid : json[0].orid,
						num : json[0].num,
						remark :json[0].remark
					});
				}
			});
			
			$('#dg').datagrid({
				idField : 'id',
				toolbar:[{
					text: '确定',
					iconCls:'icon-edit',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
						sumNum();
					}
				},'-',{
					text: '删行',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#dg').datagrid('getSelected');
						if (row){
							var index = $('#dg').datagrid('getRowIndex', row);
							$('#dg').datagrid('deleteRow', index);
						}
						sumNum();
					}
				},'-',{
					text: '保存',
					iconCls:'icon-add',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
						
						var items = [];
						var rows = $('#dg').datagrid('getRows');
						for ( var i = 0; i < rows.length; i++) {
							items.push(rows[i].id);
							items.push(rows[i].num);
						}
						$('#items').val(items.join(','));
						
						$('#transferForm').form('submit', {
							url : '${pageContext.request.contextPath}/kcgl/goodsTransferAction!add.html',
							success : function(d) {
								var json = $.parseJSON(d);
								parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
								});
								if (json.success) {
									parent.showTab("添加库存调拨单","库存调拨");
								}
							}
						});
					}
				}],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickRow:function(rowIndex){
					if (lastIndex != rowIndex){
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}else{
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}
					lastIndex = rowIndex;
					sumNum();
				}
			});
		});
	
		function _search() {
			$('#searchForm').form('submit', {
				url : '${pageContext.request.contextPath}/spgl/goodsAction!noAuth_noPageDatagrid.html',
				success : function(d) {
					var json = $.parseJSON(d);
					$('#dg').datagrid('endEdit', lastIndex);
					$('#dg').datagrid('appendRow',{
						id:json[0].id,
						type:json[0].type,
						gtname:json[0].gtname,
						bname:json[0].bname,
						name:json[0].name,
						unit:json[0].unit,
						color:json[0].color,
						size:json[0].size,
						num:'1'
					});
					lastIndex = $('#dg').datagrid('getRows').length-1;
					$('#dg').datagrid('selectRow', lastIndex);
					
					sumNum();
				}
			});
			cleanSearch();
		}
		
		function cleanSearch() {
			$('#searchForm input').val('');
		}
		
		function sumNum(){
			var rows = $('#dg').datagrid('getRows');
			var sum = 0;
			for ( var i = 0; i < rows.length; i++) {
				sum += Number(rows[i].num);
			}
			$('#num').val(sum);
		}
	</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
	
		<div data-options="region:'north',border:false" style="height: 150px; overflow: hidden;">
			<form id="transferForm" style="height:110px;">
				<input name="id" type="hidden"/>
				<input id="items" name="items" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">日期</th>
						<td style="width: 200px;"><input name="date" id="datebox" class="Wdate" onclick="WdatePicker()" style="width: 200px;" data-options="required:'true',missingMessage:'请输入调拔日期'"/></td>
						<th style="width: 80px;">单号</th>
						<td style="width: 200px;"><input name="serial" style="width: 200px;" readonly="readonly" id="serial"/></td>
						<th style="width: 80px;">操作员</th>
						<td style="width: 200px;"><input name="uid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择操作员'"/></td>
					</tr>
					<tr>
						<th> 调入仓库 </th>
						<td><input name="irid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择调入仓库'"/></td>
						<th>调出仓库</th>
						<td><input name="orid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择调出仓库'"/></td>
						<th>调拨总数</th>
						<td><input name="num" id="num" style="width: 200px;" type="text" readonly="readonly"/></td>
					</tr>
					<tr>
						<th>备注</th>
						<td colspan="5"><textarea name="remark" style="height:30px;width: 97%;"></textarea></td>
					</tr>
				</table>
			</form>
			
			<form id="searchForm" style="height:10px;margin-top:10px;">
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">商品条码</th>
						<td style="width: 200px;"><input name="barcode" style="width: 200px;" /></td>
						<th style="width: 80px;">款号/款式</th>
						<td style="width: 200px;"><input name="type" style="width: 200px;" /></td>
						<td style="width: 280px;" align="right">
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">定位商品</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr>
				</table>
			</form>
		</div>
	
		<div data-options="region:'center',border:false" style="overflow: hidden;">
			<table id="dg" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit',singleSelect: true">  
		        <thead>
		            <tr>
		            	<th data-options="field:'id',width:100,hidden:true">商品ID</th>  
		                <th data-options="field:'type',width:100">款号/款式</th>  
		                <th data-options="field:'gtname',width:100">商品类别</th>
		                <th data-options="field:'bname',width:100">品牌</th>
	                 	<th data-options="field:'name',width:100">商品名称</th>
	                  	<th data-options="field:'unit',width:100">单位</th>
                   		<th data-options="field:'color',width:100">颜色</th>
                   		<th data-options="field:'size',width:100">尺码</th>
		                <th data-options="field:'num',width:100,align:'center',editor:{type:'numberbox'}">调拔总数</th>
		            </tr>
		        </thead>
		    </table>
		</div>
	</body>
</html>